package com.leyou.auth.config;
import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {

    private String pubKeyPath;

    private String priKeyPath;

    private PublicKey publicKey;

    private PrivateKey privateKey;

    private UserTokenProperties user = new UserTokenProperties();

    private AppTokenProperties app = new AppTokenProperties();

    @Data
    public class UserTokenProperties {

        private int expire;

        private String cookieName;

        private String cookieDomain;
    }

    @Data
    public class AppTokenProperties {
        private Long id;

        private String secret;

        private int expire;

        private String headerName;
    }

    @Override
    public void afterPropertiesSet() {
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
            privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("公私钥获取失败！", e);
            throw new RuntimeException(e);
        }

    }
}
